﻿#include "JsonParser.h"

#include <qfile.h>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <qjsonarray.h>
#include <qjsonvalue.h>
#include <qvector.h>
#include <qlist.h>
#include <qdebug.h>
#include <qdir.h>
#include <qsharedpointer.h>

#include "ElaMessageBar.h"
#include "TextFileParser.h"

namespace pst
{
    HearderFileUniqueData::~HearderFileUniqueData()
    {
    }
    
    JsonParser* JsonParser::GetInstance()
    {
        static JsonParser instance;
        return &instance;
    }
    
    JsonParser::JsonParser(QObject* parent)
    {
    }
    
    JsonParser::~JsonParser()
    {
    }
    
    JsonParser::ResultInfo JsonParser::GetResultData(const QVector<QString>& filepathArray)
    {
        ResultInfo resultInfo;
        resultInfo.projectData = ProjectDataPointer::create();
        ProjectDataPointer& projectData = resultInfo.projectData;
        for (const QString& filePath : filepathArray) {
            ResultDataInfo* resultDataInfo = new ResultDataInfo();
            resultDataInfo->filePath = filePath;
            resultDataInfo->fileType = FileDataType::File2DCurve;
            TextFileParser& resultData = resultDataInfo->resultData;
            resultData.SetFileName(filePath);
            bool isSuccess = resultData.Update();
            if (isSuccess) {
                projectData->projectResultData.append(ResultDataPointer(resultDataInfo));
            }
            else {
                //resultInfo.errorMessageList.append(resultData.getErrorMessage());
                resultInfo.errorMessageList.append(filePath);
                delete resultDataInfo;
            }
        }
        if (!projectData->projectFileData.isEmpty() || !projectData->projectResultData.isEmpty()) {
            m_projectDataList.append(projectData);
        }
        return resultInfo;
    }
    
    JsonParser::ResultInfo JsonParser::GetProjectData(const QString& filePath)
    {
        ResultInfo resultInfo;
        resultInfo.projectData = ProjectDataPointer::create();
        QFile file(filePath);
        if (!file.open(QIODevice::ReadOnly)) {
            qDebug() << filePath << " open error!!!";
            return resultInfo;
        }
        QByteArray jsonText = file.readAll();
        file.close();
        QJsonParseError jsonError;
        QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonText, &jsonError);
        if (!jsonDocument.isNull() && (jsonError.error == QJsonParseError::NoError)) {
            if (jsonDocument.isArray()) {
                QJsonArray jsonArray = jsonDocument.array();
                for (int i = 0; i < jsonArray.size(); ++i) {
                    QJsonObject jsonObject = jsonArray.at(i).toObject();
                    HearderFileInfo heardFileInfo;
                    heardFileInfo.result_type = jsonObject.value("result_type").toString();
                    heardFileInfo.headfile_name = jsonObject.value("headfile_name").toString();
                    heardFileInfo.headfile_path = jsonObject.value("headfile_path").toString();
                    GetHeardData(resultInfo, heardFileInfo);
                }
            }
        }
        ProjectDataPointer& projectData = resultInfo.projectData;
        if (!projectData->projectFileData.isEmpty() || !projectData->projectResultData.isEmpty()) {
            m_projectDataList.append(projectData);
        }
        return resultInfo;
    }
    
    void JsonParser::GetHeardData(ResultInfo& resultInfo, HearderFileInfo& heardFileInfo)
    {
        QString filePath = heardFileInfo.headfile_path + "/" + heardFileInfo.headfile_name;
        QFile file(filePath);
        if (!file.open(QIODevice::ReadOnly)) {
            qDebug() << filePath << " open error!!!";
            return;
        }
        QByteArray jsonText = file.readAll();
        file.close();
        QJsonParseError jsonError;
        QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonText, &jsonError);
        if (!jsonDocument.isNull() && (jsonError.error == QJsonParseError::NoError)) {
            if (jsonDocument.isObject()) {
                QJsonObject jsonObject = jsonDocument.object();
                if (GetHeardUniqueData(resultInfo, heardFileInfo.hearderFileData, jsonObject)) {
                    resultInfo.projectData->projectFileData.append(std::move(heardFileInfo));
                }
            }
        }
    }
    
    bool JsonParser::GetHeardUniqueData(ResultInfo& resultInfo, HearderFileData& hearderFileData, const QJsonObject& jsonObject)
    {
        bool isSuccess = false;
        hearderFileData.datafile_type = jsonObject.value("datafile_type").toString();
        hearderFileData.resultdata_type = jsonObject.value("resultdata_type").toString();
        QJsonArray keyArray = jsonObject.value("key").toArray();
        for (int i = 0; i < keyArray.size(); ++i) {
            hearderFileData.key.append(keyArray.at(i).toString());
        }
        hearderFileData.datafolder_path = jsonObject.value("datafolder_path").toString();
        QString& resultdata_type = hearderFileData.resultdata_type;
        if (resultdata_type == "S_PARAMETER") {
            SHearderFile* sHearderFile = new SHearderFile();
            QJsonObject unitObject = jsonObject.value("unit").toObject();
            sHearderFile->unit.frequency = unitObject.value("frequency").toString();
            sHearderFile->data_type = jsonObject.value("data_type").toString();
            sHearderFile->datafile_name = jsonObject.value("datafile_name").toString();
            QString filePath = hearderFileData.datafolder_path + "/" + sHearderFile->datafile_name;
            ResultDataInfo* resultDataInfo = new ResultDataInfo();
            resultDataInfo->filePath = filePath;
            resultDataInfo->fileType = FileDataType::File2DCurve;
            TextFileParser& resultData = resultDataInfo->resultData;
            resultData.SetFileName(filePath);
            isSuccess = resultData.Update();
            if (isSuccess) {
                sHearderFile->resultDataInfo = ResultDataPointer(resultDataInfo);
                hearderFileData.unique_data = uniqueDataPointer(sHearderFile);
                resultInfo.projectData->projectResultData.append(sHearderFile->resultDataInfo);
                }
            else {
                resultInfo.errorMessageList.append(resultData.getErrorMessage());
                delete resultDataInfo;
                delete sHearderFile;
            }
        }
        else {
            OtherHearderFile* otherHearderFile = new OtherHearderFile();
            otherHearderFile->datafile_name = jsonObject.value("datafile_name").toString();
            QString filePath = hearderFileData.datafolder_path + "/" + otherHearderFile->datafile_name;
            ResultDataInfo* resultDataInfo = new ResultDataInfo();
            resultDataInfo->filePath = filePath;
            resultDataInfo->fileType = FileDataType::File2DCurve;
            TextFileParser& resultData = resultDataInfo->resultData;
            resultData.SetFileName(filePath);
            isSuccess = resultData.Update();
            if (isSuccess) {
                otherHearderFile->resultDataInfo = ResultDataPointer(resultDataInfo);
                hearderFileData.unique_data = uniqueDataPointer(otherHearderFile);
                resultInfo.projectData->projectResultData.append(otherHearderFile->resultDataInfo);
            }
            else {
                resultInfo.errorMessageList.append(resultData.getErrorMessage());
                delete resultDataInfo;
                delete otherHearderFile;
            }
        }
        return isSuccess;
    }
}
